package threadPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-12-20 17:58
 **/
public class T14_ParallelStreamAPI {


    /**
     * 具体哪种更高效不能一概而论
     * parallelStream 并行处理但是拆分任务也需要系统资源
     * --------------------------------------------------------------------
     * 研发过程中需要按照当前用户量和未来的规划做模拟测试，找出最优的解决方案
     * --------------------------------------------------------------------
     */

    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            nums.add(1000000 + random.nextInt(1000000));
        }
        long start = System.currentTimeMillis();
        nums.forEach(T14_ParallelStreamAPI::isPrime);
        long end = System.currentTimeMillis();
        System.out.println("直接处理需要的时间：====> " + (end - start));

        start = System.currentTimeMillis();
        nums.stream().forEach(T14_ParallelStreamAPI::isPrime);
        end = System.currentTimeMillis();
        System.out.println("流式处理需要的时间：====> " + (end - start));

        start = System.currentTimeMillis();
//        并行流 parallelStream
        nums.parallelStream().forEach(T14_ParallelStreamAPI::isPrime);
        end = System.currentTimeMillis();
        System.out.println("并行流式处理需要的时间：====> " + (end - start));


    }

    private static void isPrime(Integer integer) {
//        try {
//            Thread.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }


}
